{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串格式化\n",
    "- 把字符串按照一定格式打印或者填充\n",
    "- 格式化有两种方式\n",
    "    - 利用百分号 %\n",
    "    - 利用format函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = \"我可能哟啊离开了\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 利用百分号格式化\n",
    "- 在字符串中，利用%表示一个特殊意义，表示对字符串进行格式化\n",
    "- %d: 此处应放入一个整数\n",
    "- %s: 此处应放入一个字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I love %s\n"
     ]
    }
   ],
   "source": [
    "s = \"I love %s\"\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wo taoyan you\n"
     ]
    }
   ],
   "source": [
    "print(\"wo taoyan %s\"%\"you\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I love myself\n"
     ]
    }
   ],
   "source": [
    "print(s%\"myself\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am %d years old\n",
      "I am 18 years old\n"
     ]
    }
   ],
   "source": [
    "s = \"I am %d years old\"\n",
    "print(s)\n",
    "print(s%18)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am %s and my cardId %d\n",
      "I am zlf and my cardId 12\n"
     ]
    }
   ],
   "source": [
    "s = \"I am %s and my cardId %d\"\n",
    "print(s)\n",
    "#如果字符串中有占位符，那么有几个占位符必须要有几个实际内容代替，或者一个也不要\n",
    "#如果出现多个占位符，则相应内容需要用括号括起来\n",
    "print(s%(\"zlf\",12))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### format函数格式化字符串\n",
    "- 直接用format函数进行格式化\n",
    "- 推荐使用此为方法\n",
    "- 以{}和: 代替%号，后面用format带参数为完成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I like myself\n",
      "Yes , I just love myself,can't love who\n"
     ]
    }
   ],
   "source": [
    "s = \"I like {}\".format(\"myself\")\n",
    "print(s)\n",
    "s = \"Yes , I just love {0},can't love {1}\".format(\"myself\",\"who\")\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# None\n",
    "- 表示什么都没有\n",
    "- 如果函数没有返回值，可以返回None\n",
    "- 用来占位\n",
    "- 用来接触变量绑定"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 表达式\n",
    "- 由一个或者几个数字或者变量和运算符组成的一行代码\n",
    "- 通常会返回一个结果\n",
    "# 运算符\n",
    "- 由一个以上的值经过变化得到新值得过程叫运算\n",
    "- 用于运算的符号叫运算符\n",
    "- 运算符分类\n",
    "    - 算数运算符\n",
    "    - 比较、关系运算符\n",
    "    - 复制运算符\n",
    "    - 逻辑运算符\n",
    "    - 位运算\n",
    "    - 成员运算\n",
    "    - 身份运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 算数运算符\n",
    "- 进行算数运算的返符号\n",
    "- python没有自增自减运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11\n",
      "36\n",
      "2.25\n",
      "1\n",
      "2\n",
      "6561\n",
      "27\n"
     ]
    }
   ],
   "source": [
    "# +、 - 加减\n",
    "a = 9 + 3 - 1\n",
    "print(a)\n",
    "# * 乘\n",
    "a = 9 * 4 \n",
    "print(a)\n",
    "# / 除\n",
    "# 在py2.x和py3.x中，除号（/）结果可能不一致，此处以3.x为准\n",
    "a = 9 / 4\n",
    "print(a)\n",
    "\n",
    "\n",
    "#两数相除既有余数也有商数\n",
    "# % 取余运算\n",
    "a = 9 % 4\n",
    "print(a)\n",
    "\n",
    "# //  表示取商运算，也叫地板除\n",
    "a = 9 // 4\n",
    "print(a)\n",
    "\n",
    "# ** 表示幂运算\n",
    "a = 9 ** 4\n",
    "print(a)\n",
    "\n",
    "a = 3 ** 3\n",
    "print(a)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 比较运算符\n",
    "- 对两个变量或者值进行比较运算的运算符\n",
    "- 比较的结果是布尔值，即 true 、 false"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# == ， 等于号\n",
    "a = 3 ** 4\n",
    "# 下面语句执行顺序是\n",
    "# 1， 计算 a == 80\n",
    "# 2， 把结果放入b中\n",
    "b = a == 80\n",
    "print(b)\n",
    "\n",
    "# != 不等于\n",
    "print(9 != 10)\n",
    "# > 大于\n",
    "# < 小于\n",
    "# >= 大于等于\n",
    "# <= 小于等于"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 赋值运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "# = ，赋值\n",
    "a = 0\n",
    "c = a = 4\n",
    "\n",
    "# += ,缩写\n",
    "a = 0 \n",
    "a += 7 # a = a + 7\n",
    "print(a)\n",
    "# 所有数学运算符都有缩写形式\n",
    "# -=, x=, /= ,//=, %=, **= \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 逻辑运算符\n",
    " - 对布尔值进行计算的符号\n",
    " - and 逻辑与\n",
    " - or  逻辑或\n",
    " - not 逻辑非\n",
    " - python 中没有逻辑运算没有异或运算\n",
    " - 运算规则：\n",
    "     - and  看做乘法，or看做加法\n",
    "     - True 看做 1，false看做 0\n",
    "     - 则逻辑运算就能转换成证书数学运算\n",
    "     - 最后结果是0则为false否则为True\\\n",
    "- 逻辑运算的短路问题\n",
    "    - 逻辑运算，按运算顺序计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 逻辑运算符\n",
    "a = True\n",
    "b = False\n",
    "c = True\n",
    "# 以下表达式等价于： d = 1 * 0 + 1\n",
    "d = a and b or c\n",
    "print(d)\n",
    "\n",
    "d = a or b and a\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-1-055b185d45a8>, line 8)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-1-055b185d45a8>\"\u001b[0;36m, line \u001b[0;32m8\u001b[0m\n\u001b[0;31m    a == a or (b=9) and 6\u001b[0m\n\u001b[0m                ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 逻辑运算的短路问题案例\n",
    "\n",
    "# 下面的逻辑表达式，a的值一定是True 则运行到Or的时候，整个表达式不在向下计算\n",
    "a = True or xx\n",
    "# 下面表达式，如果xx中包含赋值表达式，则结果很难预测\n",
    "#示例（伪代码）\n",
    "b = 0 \n",
    "a == a or (b=9) and 6\n",
    "# 假定上面的表达式如果没有语法错误\n",
    "#则b最终的值应该是0而不是9\n",
    "print(b)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 成员运算符号\n",
    "- 用来检测某一个变量是否是另一个变量的成员\n",
    "- in\n",
    "- not in"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 案例\n",
    "l = [1,2,3,4,5]\n",
    "a = 7\n",
    "\n",
    "b = a in l\n",
    "print(b)\n",
    "\n",
    "a = 4 \n",
    "print(a in l)\n",
    "\n",
    "print(a not in l)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 身份运算符\n",
    "- is: 用来检测两个变量是否是同一个变量\n",
    "    - 语法就是 var1 is var2 \n",
    "- is not: 两个变量不是同一个变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "a = 9 \n",
    "b = 9\n",
    "print(a is b)\n",
    "a = \"wo ai wo zi ji\"\n",
    "b = \"wo ai wo zi ji\"\n",
    "print(a is b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 运算符的优先级问题\n",
    "- 永远记住，括号具有最高优先级\n",
    "- 优先级表格\n",
    "    - ** 指数（最高优先级）\n",
    "    - ~ ＋ - 按位翻转，一元加号和减号（最后两个的方法名为＋@和 -@）\n",
    "    - * / % //    乘，除，取模和取整除\n",
    "    - ＋ - 加法减法\n",
    "    - ＞＞＜＜ 右移，左移运算符\n",
    "    - & 位 ＇AND'\n",
    "    - ^ |  位运算符\n",
    "    - <＝ ＜ ＞ ＞＝  比较运算符\n",
    "    - <> ＝= !＝     等于运算符\n",
    "    - = %＝ ／= //= -＝ +＝ *= **=  赋值运算符\n",
    "    - is  is not     身份运行\n",
    "    - In not in      成员运算符\n",
    "    - not or and     逻组运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 程序结构\n",
    "- 顺序\n",
    "- 分支\n",
    "- 循环"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
